﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DevEx.Thanh.Class
{
    internal class CRoleMenuController
    {
        private QLVTDataContext objDataContext = new QLVTDataContext();

        public int InsertRecord(tblRoleMenu LocalTable)
        {
            try
            {
                objDataContext.tblRoleMenus.InsertOnSubmit(LocalTable);
                objDataContext.SubmitChanges();
                return LocalTable.ID_RoleMenu;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public void DeleteAllByRole(int _idRole)
        {
            try
            {
                objDataContext.tblRoleMenus.DeleteAllOnSubmit(from r in objDataContext.tblRoleMenus
                                                              where r.ID_Role == _idRole
                                                              select r);
                objDataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool CheckMenuInRole(int idRole, int idMenu)
        {
            //cach 1
            return objDataContext.tblRoleMenus.Any(m => m.ID_Menu == idMenu & m.ID_Role == idRole);
        }

        public bool CheckMenuInRoleByName(int idRole, string menuText)
        {
            //cach 1
            return objDataContext.tblRoleMenus.Any(m => m.tblMenu.MenuText == menuText & m.ID_Role == idRole);
        }

        public CRoleMenuController()
        {
            objDataContext = new QLVTDataContext();
        }

        public CRoleMenuController(QLVTDataContext _obj)
        {
            objDataContext = _obj;
        }

    }
}
